package cn.sypt.yuhao.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import cn.sypt.yuhao.domain.Suibi;
import cn.sypt.yuhao.domain.Video;

import com.mysql.jdbc.Connection;

public class GrglBO {
	
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	
	public boolean updateUser(String id,String uname,String uemail,String uaddress,String ubirth,String uphone)
	{
		boolean a=false;
		try {
			
	        con=(Connection) new DBcon().getConnection();
			
			String sql="update user set uname='"+uname+"',uemail='"+uemail+"',uaddress='"+uaddress+"',ubirth='"+ubirth+"',uphone='"+uphone+"' where uid='"+id+"'";
			
			ps=con.prepareStatement(sql);
			
			int num=ps.executeUpdate();
			
			if(num==1)
			{
				a=true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();					
		}finally
		{
			this.close();
		}
		
		return a;
		
		}
	
	
	public boolean czmm(String upass,String uid)
	{
		boolean a=false;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="update user set upass='"+upass+"' where uid='"+uid+"'";
			
			ps=con.prepareStatement(sql);
			
			int num=ps.executeUpdate();
			
			if(num==1)
			{
				a=true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return a;
	}
	
	//获得点赞视频数的总页数
	public int getdzpageCount(int pageSize,String uid)
	{
		
		String sql="";
		int pageCount=0;
		try {
			
			con=(Connection) new DBcon().getConnection();
			
			
			sql="select count(*) from zan where uid='"+uid+"'";
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next())
			{
				int rowCount=rs.getInt(1);
				System.out.println("rowCount="+rowCount);
				if(rowCount%pageSize==0)
				{
					pageCount=rowCount/pageSize;
				}else
				{
					pageCount=rowCount/pageSize+1;
				}
				
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}finally
		{
			this.close();
		}
		
		return pageCount;
		
	}
	
	//获得所有点赞视频
	public ArrayList getDzspid(String uid,int pageSize,int pageNow)
	{
		ArrayList al=new ArrayList();
		int s_pageNow=(pageNow-1)*pageSize;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="select zan.spid from zan,video where zan.uid='"+uid+"' and video.spsh=1 and zan.spid=video.spid order by ztime desc limit "+s_pageNow+","+pageSize+"";
			
			ps=con.prepareStatement(sql);
			
			rs=ps.executeQuery();
			while(rs.next())
			{
				String spid=rs.getString(1);
				
				al.add(spid);
			}
				
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		return al;
	}
	
	    //获得下载视频的总页数
		public int getxzpageCount(int pageSize,String uid)
		{
			
			String sql="";
			int pageCount=0;
			try {
				
				con=(Connection) new DBcon().getConnection();
				
				
				sql="select count(*) from download where uid='"+uid+"'";
				ps=con.prepareStatement(sql);
				rs=ps.executeQuery();
				if(rs.next())
				{
					int rowCount=rs.getInt(1);
					System.out.println("rowCount="+rowCount);
					if(rowCount%pageSize==0)
					{
						pageCount=rowCount/pageSize;
					}else
					{
						pageCount=rowCount/pageSize+1;
					}
					
				}
				
				
				
			} catch (Exception e) {
				e.printStackTrace();
				
			}finally
			{
				this.close();
			}
			
			return pageCount;
			
		}
	
	//获得下载所有视频的id
	public ArrayList getxzspid(String uid,int pageSize,int pageNow)
	{
		ArrayList al=new ArrayList();
		int s_pageNow=(pageNow-1)*pageSize;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="select download.spid from download,video where download.uid='"+uid+"' and video.spsh=1 and download.spid=video.spid GROUP BY spid order by xztime desc limit "+s_pageNow+","+pageSize+"";
			
			ps=con.prepareStatement(sql);
			
			rs=ps.executeQuery();
			while(rs.next())
			{
				String spid=rs.getString(1);
				
				al.add(spid);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return al;
	}
	
	
	//根据视频id获得所有视频
	public ArrayList getVideo(ArrayList bl)
	{
		ArrayList al=new ArrayList();
		
		try {
			for(int i=0;i<bl.size();i++)
			{
				String spid=(String) bl.get(i);
				con=(Connection) new DBcon().getConnection();
				String sql="select * from video where spid='"+spid+"'";
				
				ps=con.prepareStatement(sql);
				
				rs=ps.executeQuery();
				while(rs.next())
				{
					Video video=new Video();
					video.setSpid(rs.getString(1));
					video.setSpname(rs.getString(2));
					video.setSpstr(new Sdzfc().getSub(rs.getString(3)));
					
					video.setSptime(rs.getString(4));
					video.setSpurl(rs.getString(5));
					video.setSptpurl(rs.getString(6));
					video.setSpxz(rs.getInt(7));
					video.setSpdz(rs.getInt(8));
					video.setBid(rs.getString(9));
					al.add(video);
				}
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return al;
		
	}
	
	//����ͷ��
	public boolean updateTx(String uid,String uimage)
	{
		boolean a=false;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="update user set uimage='"+uimage+"' where uid='"+uid+"'";
			
			ps=con.prepareStatement(sql);
			
			int num=ps.executeUpdate();
			
			if(num==1)
			{
				a=true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return a;
	}
	
	//增加随笔
	public boolean addSuibi(String sbid,String uid,String sbtime,String sbtext)
	{
		boolean a=false;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="insert into suibi values('"+sbid+"','"+uid+"','"+sbtime+"','"+sbtext+"');";
			
			ps=con.prepareStatement(sql);
			
			int num=ps.executeUpdate();
			
			if(num==1)
			{
				a=true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return a;
	}
	
	//删除随笔
	public boolean dlSuibi(String sbid)
	{
		boolean a=false;
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="delete from suibi where sbid='"+sbid+"'";
			
			ps=con.prepareStatement(sql);
			
			
			
			int num=ps.executeUpdate();
			
			if(num==1)
			{
				a=true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return a;
	}
	
	//ɾ����޺����ص���Ƶ
	public boolean dlDzXzsp(String flag1,String spid)
	{
		boolean a=false;
		String sql="";
		try {
			
			con=(Connection) new DBcon().getConnection();
			
			
			if(flag1.equals("dldz"))
			{
				sql="delete from zan where spid='"+spid+"'";
			}
			if(flag1.equals("dlxz"))
			{
				sql="delete from download where spid='"+spid+"'";
			}
			ps=con.prepareStatement(sql);
			
			int num=ps.executeUpdate();
			
			if(num==1)
			{
				a=true;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return a;
	}
	
    //获得随笔
	public ArrayList getSuibi(String uid)
	{
		ArrayList al=new ArrayList();
		try {
			
			con=(Connection) new DBcon().getConnection();
			String sql="select * from suibi where uid='"+uid+"' order by sbtime desc";
			
			ps=con.prepareStatement(sql);
			
			rs=ps.executeQuery();
			
			while(rs.next())
			{
				Suibi sb=new Suibi();
				sb.setSbid(rs.getString(1));
				sb.setUid(rs.getString(2));
				sb.setSbtime(rs.getString(3));
				sb.setSbtext(rs.getString(4));
				al.add(sb);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
		
		return al;
	}
	
	//获得用户上传视频总页数
	public int getscsppageCount(int pageSize,String uname)
	{
		
		String sql="";
		int pageCount=0;
		try {
			
			con=(Connection) new DBcon().getConnection();
			
			
			sql="select count(*) from video where uname='"+uname+"'";
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next())
			{
				int rowCount=rs.getInt(1);
				System.out.println("rowCount="+rowCount+",uname="+uname);
				if(rowCount%pageSize==0)
				{
					pageCount=rowCount/pageSize;
				}else
				{
					pageCount=rowCount/pageSize+1;
				}
				
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}finally
		{
			this.close();
		}
		
		return pageCount;
		
	}
	
	//用户上传视频的集合
	public ArrayList scspJh(String uname,int pageNow,int pageSize)
	{
	
		int s_pageNow=(pageNow-1)*pageSize;
		ArrayList al=new ArrayList();
		try {			
			con=(Connection) new DBcon().getConnection();
			String sql="select spid, spname,spstr,sptime,spsh,sptpurl from video where uname='"+uname+"' and (spsh=1 or spsh=0) order by sptime desc limit "+s_pageNow+","+pageSize+"";
			
			ps=con.prepareStatement(sql);
			
			rs=ps.executeQuery();
			while(rs.next())
			{
				Video video=new Video();
				video.setSpid(rs.getString(1));
				video.setSpname(rs.getString(2));
				video.setSpstr(new Sdzfc().getSub(rs.getString(3)));
				video.setSptime(rs.getString(4));
				video.setSpsh(rs.getInt(5));
				video.setSptpurl(rs.getString(6));
				al.add(video);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally
		{
			this.close();
		}
	
		
		return al;
	}
	
	//�رս��
	public void close()
	{
		try {
			
			if(rs!=null)
			{
				rs.close();
				rs=null;
			}
			if(ps!=null)
			{
				ps.close();
				ps=null;
			}
			if(con!=null)
			{
				con.close();
				con=null;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
		
		
	}


